//
// Created by sgyt on 2020/2/9.
// times 函数demo
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/times.h>

int main(int argc,char *argv[])
{
    struct tms time_buf_head,time_buf_end;
    long tck = 0;
    clock_t time_head,time_end;

    //获取系统时钟1秒钟有多少个
    tck = sysconf(_SC_CLK_TCK);

    //经常运行到此时的系统时钟数（总数）
    time_head = times(&time_buf_head);
    if(time_head < 0)
    {
        perror("times is error\n");
        exit(0);
    }
    //此时进程所处的时间点（秒）
    printf("time_head is [%d]\n",time_head);
    printf("time_head is [%f]\n",time_head/(double)tck);

    sleep(2);

    //经常到此时的系统时钟数
    time_end = times(&time_buf_end);
    if(time_end < 0)
    {
        perror("times is error\n");
        exit(0);
    }
    //此时进程所处的时间点（秒）
    printf("time_end is [%f]\n",time_end/(double)tck);
    printf("user time is [%f]\n ",(time_buf_end.tms_utime - time_buf_head.tms_utime)/(double)tck);
    printf("systime time is [%f]\n ",(time_buf_end.tms_stime - time_buf_head.tms_stime)/(double)tck);
    printf("child user time is [%f]\n ",(time_buf_end.tms_cutime - time_buf_head.tms_cutime)/(double)tck);
    printf("child sys time is [%f]\n ",(time_buf_end.tms_cstime - time_buf_head.tms_cstime)/(double)tck);
    return 0;
}

